<?php
    /**
     * cms后台案例管理页
     * author wang
     */

define("PHP_IN",true);
require "../include/init.php";
require "checklogin.php";
require ROOT_PATH."cms/smart_connection.php";

$page = isset($_GET['page']) ? $_GET['page'] : 1 ;
$pagesize = 4;
$pagestart = ($page-1) * $pagesize ;

$sql = "SELECT count(*) AS num FROM cases";
$result = $db->query($sql);
$row = $db->fetch_one($result);
$count = $row['num'];


$pagecount = ceil($count/$pagesize);
$page_links = cms_page_links($pagecount, $page, "case_list.php?$page");

$sql = "SELECT * FROM cases INNER JOIN case_type ON cases.cid = case_type.cid ORDER BY caseid DESC LIMIT $pagestart,$pagesize ";
$result = $db->query($sql);
$cases = $db->fetch_all($result);

if(isset($_GET['act']) && $_GET['act']=="del"){
    $deleteid = isset($_GET['deleteid']) ? $_GET['deleteid'] : 0;
    $sql = "DELETE FROM cases WHERE caseid = $deleteid";
    $result = $db->query("$sql");
    if($result){
        $sql = "SELECT count(*) AS num FROM cases";
        $result = $db->query($sql);
        $row = $db->fetch_one($result);
        $count = $row['num'];

        if($page==1){
            $page==1;
        }else{
            if($page == $pagecount){
                if($pagecount_now < $pagecount){
                    $page = $pagecount_now;
                }
            }
        }

        header("Location:case_list.php?page=".$page);
    }else{
        alert("删除失败");
    }
}

if(isset($_GET['act']) && $_GET['act']=="delall" && !empty($_POST['idarr'])){
    $deleteid = isset($_POST['idarr']) ? implode(",",$_POST['idarr']) : "";
    $sql = "DELETE FROM cases WHERE caseid IN ($deleteid)";
    $result = $db->query("$sql");
    if($result){
        $sql = "SELECT count(*) AS num FROM cases";
        $result = $db->query($sql);
        $row = $db->fetch_one($result);
        $count = $row['num'];

        $pagecount_now = ceil($count/$pagesize);
        if($page==1){
            $page==1;
        }else{
            if($page == $pagecount){
                if($pagecount_now < $pagecount){
                    $page = $pagecount_now;
                }
            }
        }

        header("Location:case_list.php?page=".$page);
    }else{
        alert("删除失败");
    }
}

$smarty->assign("cases",$cases);
$smarty->assign("page",$page);
$smarty->assign("pagecount",$pagecount);
$smarty->assign("pagesize",$pagesize);
$smarty->assign("page_links",$page_links);

$smarty->display("case_list.tpl");
$db->close();
?>
